﻿#include "datachange.h"





datachange::datachange(QObject *parent) : QObject(parent)
{
}

datachange::~datachange()
{
    for (int i = 0; i < chanel; i++) {
        delete []m_complexdata_in[i];
        delete []m_complexdata_out[i];
    }
    delete []m_complexdata_in;
    delete []m_complexdata_out;
}

void datachange::creatchange()
{
    m_fft = new fft();
    m_complexdata_in = new Complex*[chanel];   //申请chanel*dataLen个complex
    m_complexdata_out = new Complex*[chanel];
    for (int i = 0; i < chanel; i++) {
        m_complexdata_in[i] = new Complex[dataLen];
        m_complexdata_out[i] = new Complex[dataLen];
        memset(m_complexdata_in[i], 0, dataLen * sizeof(Complex));
        memset(m_complexdata_out[i], 0, dataLen * sizeof(Complex));
    }
}


void datachange::startchange(char *tempsonardatabuf)
{
    //qDebug() << "startchange";
    QVector<QVector<double>> x(chanel), y(chanel) ; //二维数组的 QVector 定义
    int i, j;
    for(j = 0; j < chanel; j++) {       //通道数
        for(i = 0; i < dataLen; i++) {  //每个通道取的点数
            m_complexdata_in[j][i].rl = tempsonardatabuf[i * 160 + j * 4 + 0] | tempsonardatabuf[i * 160 + j * 4 + 1] << 8; //0字节和1字节组成通道零的第一个实部点
            m_complexdata_in[j][i].im = tempsonardatabuf[i * 160 + j * 4 + 2] | tempsonardatabuf[i * 160 + j * 4 + 3] << 8; //2字节和3字节组成通道零的第一个虚部点
        }
        m_fft->fft1(m_complexdata_in[j], dataLen, m_complexdata_out[j]); //傅里叶变换
    }
    for(j = 0; j < chanel; j++) {
        for(i = 0; i < dataLen; i++) {
            x[j] << m_complexdata_out[j][i].im;
            y[j] << m_complexdata_out[j][i].rl;
        }
    }
    emit endchange(x, y);
}
